﻿using CompanyManageSystem.Model;
using CompanyManageSystem.Service;
using CompanyManageSystem.UI.WeiXin;
using DevExpress.Utils.OAuth;
using DevExpress.Utils.OAuth.Provider;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CompanyManageSystem.UI.Info
{
    public partial class Frm_Manager : Form
    {
        #region 构造方法
        /// <summary>
        /// 构造方法
        /// </summary>
        public Frm_Manager()
        {
            InitializeComponent();
        }
        #endregion

        #region 窗体加载完成
        /// <summary>
        /// 窗体加载完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Frm_Manager_Load(object sender, EventArgs e)
        {
            AppService aservice = new AppService();
            ManagerGrid.DataSource = aservice.GetManagers();

            barButtonItem6.Caption = GetIPAddress();
        }
        #endregion

        #region 获取本机IP地址(IPv4)
        /// <summary>
        /// 获取本机IP地址(IPv4)
        /// </summary>
        /// <returns></returns>
        private string GetIPAddress()
        {
            string hostName = Dns.GetHostName();
            IPHostEntry localhost = Dns.GetHostByName(hostName);  //获取IPv4地址
            IPAddress localaddr = localhost.AddressList[0];
            return localaddr.ToString();
        }
        #endregion

        #region 指示器编号
        /// <summary>
        /// 指示器编号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManagerView_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }
        #endregion

        #region 工具栏
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Frm_AddManager frm = new Frm_AddManager(status: Frm_AddManager.EStatus.Add);
            frm.SaveEvent += Manager_AddSaveEvent;
            frm.ShowDialog();
        }
        private void Manager_AddSaveEvent(Object sender, EventArgs e)
        {
            AppService aservice = new AppService();
            ManagerGrid.DataSource = aservice.GetManagers();
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Manager focusManager = ManagerView.GetFocusedRow() as Manager;

            Frm_AddManager frm = new Frm_AddManager(managerID: focusManager.ID, status: Frm_AddManager.EStatus.Modify);
            frm.SaveEvent += Manager_ModifySaveEvent;
            frm.ShowDialog();
        }
        private void Manager_ModifySaveEvent(Object sender, EventArgs e)
        {
            Manager man = sender as Manager;
            if (man != null)
            {
                ManagerView.SetFocusedRowCellValue("Name", man.Name);
                ManagerView.SetFocusedRowCellValue("Password", man.Password);
                ManagerView.SetFocusedRowCellValue("Photo", man.Photo);
            }
            else
            {
                AppService aservice = new AppService();
                ManagerGrid.DataSource = aservice.GetManagers();
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Manager focusManager = ManagerView.GetFocusedRow() as Manager;
            if (DialogResult.OK == MessageBox.Show("是否删除【" + focusManager.Name + "】管理员?", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information))
            {
                //确定
                AppService aservice = new AppService();
                aservice.DelManager(focusManager.ID);
                ManagerGrid.DataSource = aservice.GetManagers();
            }
            else
            {
                //取消
            }
        }
        #endregion

        #region 右键菜单
        #region 右键事件方法(未使用)
        //private void gridView1_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        //{
        //    if (e.HitInfo.InRow || e.HitInfo.InRowCell)
        //        this.popupMenu2.ShowPopup(MousePosition);
        //}
        #endregion

        /// <summary>
        /// 右键事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManagerGrid_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                popupMenu1.ShowPopup(Control.MousePosition);
            }
        }

        /// <summary>
        /// 群发微信消息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem7_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //获取焦点行
            Manager manager = ManagerView.GetFocusedRow() as Manager;

            Frm_WeiXin frm = new Frm_WeiXin(manager.Name,manager.ID);
            frm.ShowDialog();
        }

        /// <summary>
        /// 查看图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem8_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            AppService aservice = new AppService();
            Manager manager = ManagerView.GetFocusedRow() as Manager;
            var photo = aservice.GetPhotoByID(manager.ID);

            if (photo == null)
            {
                MessageBox.Show("此管理员暂无图片");
            }

            //只是查看的时候修改图片大小，其数据并未保存到数据库
            Frm_ShowPicture.ShowImage(Frm_ShowPicture.ChangePhotoSize(manager.Photo));
        }

        /// <summary>
        /// 修改图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem9_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            AppService aservice = new AppService();
            PSContext _db = new PSContext();
            Manager focusManager = ManagerView.GetFocusedRow() as Manager;
            var dialog = new OpenFileDialog();
            dialog.Title = "打开图片";
            dialog.Filter = "图片文件|*.jpg;*.jpeg;*.png;*.bmp";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                var manager = aservice.GetManagerByID(focusManager.ID);
                manager.Photo = File.ReadAllBytes(dialog.FileName);
                //修改图片大小
                manager.Photo = Frm_ShowPicture.ChangePhotoSize(manager.Photo);
                aservice.DataSave();
                ManagerView.SetFocusedRowCellValue("Photo",manager.Photo);
                MessageBox.Show("修改成功!");
            }

            //OpenFileDialog open = new OpenFileDialog();
            //open.Title = "打开文件";
            //open.ShowHelp = true;
            //open.Filter = "图片文件（*.jpg;*.gif;*.bmp;*.png）|*.jpg;*.gif;*.bmp;*.png";
            //open.FilterIndex = 1;
            //open.RestoreDirectory = false;
            //open.InitialDirectory = "C:\\";
            //open.Multiselect = false;
            //if (open.ShowDialog() == DialogResult.OK)
            //{
            //    pictureEdit1.EditValue = ChangePictureSize(File.ReadAllBytes(open.FileName));
            //    this.PictureByte = File.ReadAllBytes(open.FileName);
            //}
        }
        #endregion

    }
}
